#  -------------------------------------------------------------------------
#  Copyright (C) 2018 BMW Car IT GmbH
#  -------------------------------------------------------------------------
#  This Source Code Form is subject to the terms of the Mozilla Public
#  License, v. 2.0. If a copy of the MPL was not distributed with this
#  file, You can obtain one at https://mozilla.org/MPL/2.0/.
#  -------------------------------------------------------------------------

# Use like this with find_package:
# find_package(ramses-shared-lib <RAMSES-VERSION>)

INCLUDE(FindPackageHandleStandardArgs)
INCLUDE(CMakeFindDependencyMacro)
SET(ramses-shared-lib_VERSION @RAMSES_VERSION@)

@PACKAGE_INIT@

FIND_PACKAGE(PkgConfig QUIET)

# Process COMPONENT parameters

LIST(LENGTH ramses-shared-lib_FIND_COMPONENTS COMPONENTS_LENGTH)
if(COMPONENTS_LENGTH GREATER 0)
    MESSAGE(FATAL_ERROR "Unused components passed: ${ramses-shared-lib_FIND_COMPONENTS}")
endif()

set(ramses-shared-lib_INCLUDE_DIRS "@PACKAGE_RAMSES_INSTALL_HEADERS_PATH@")

# find library
IF(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")

    # no shlib versioning support on windows
    FIND_LIBRARY(ramses-shared-lib_FULL_LIBRARIES
        NAMES "ramses-shared-lib"
        HINTS @PACKAGE_RAMSES_INSTALL_RUNTIME_PATH@
    )

ELSEIF(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")

    # require exact shlib version
    FIND_LIBRARY(ramses-shared-lib_FULL_LIBRARIES
        NAMES "libramses-shared-lib.so.@RAMSES_VERSION_MAJOR@.@RAMSES_VERSION_MINOR@"
        HINTS @PACKAGE_RAMSES_INSTALL_LIBRARY_PATH@
    )

ELSEIF(${CMAKE_SYSTEM_NAME} STREQUAL "Android")

    # no shlib versioning support on Android
    FIND_LIBRARY(ramses-shared-lib_FULL_LIBRARIES
        NAMES "libramses-shared-lib"
        HINTS @PACKAGE_RAMSES_INSTALL_LIBRARY_PATH@
        CMAKE_FIND_ROOT_PATH_BOTH
    )

ELSE()
    MESSAGE(FATAL_ERROR "Unsupported system: ${CMAKE_SYSTEM_NAME}, can't find shared lib")
ENDIF()


# find headless to add as dependency
find_package(ramses-shared-lib-headless REQUIRED)

FIND_PACKAGE_HANDLE_STANDARD_ARGS(ramses-shared-lib
    FOUND_VAR ramses-shared-lib_FOUND
    REQUIRED_VARS ramses-shared-lib_FULL_LIBRARIES ramses-shared-lib-headless_LIBRARIES ramses-shared-lib_INCLUDE_DIRS
    VERSION_VAR ramses-shared-lib_VERSION)

set(ramses-sdk_VERSION "@ramses-sdk_VERSION@" CACHE STRING "Ramses version" FORCE)

set(ramses-shared-lib_LIBRARIES ${ramses-shared-lib-headless_LIBRARIES} ${ramses-shared-lib_FULL_LIBRARIES})

message(STATUS "Found ramses-shared-lib libs: ${ramses-shared-lib_LIBRARIES}")
message(STATUS "Found ramses-shared-lib includes: ${ramses-shared-lib_INCLUDE_DIRS}")


MARK_AS_ADVANCED(
    ramses-shared-lib_INCLUDE_DIRS
    ramses-shared-lib_LIBRARIES
    ramses-shared-lib_FOUND
)

if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.13")
    if (NOT TARGET ramses-ramses-shared-lib)
        add_library(ramses-ramses-shared-lib SHARED IMPORTED GLOBAL)
        set_target_properties(ramses-ramses-shared-lib PROPERTIES IMPORTED_LOCATION ${ramses-shared-lib_FULL_LIBRARIES})

        if (MSVC)
            set_target_properties(ramses-ramses-shared-lib PROPERTIES IMPORTED_IMPLIB ${ramses-shared-lib_FULL_LIBRARIES})
        endif()
        target_include_directories(ramses-ramses-shared-lib INTERFACE ${ramses-shared-lib_INCLUDE_DIRS})
        target_link_libraries(ramses-ramses-shared-lib INTERFACE ramses::headless)

        if (NOT TARGET ramses::ramses)
            add_library(ramses::ramses ALIAS ramses-ramses-shared-lib)
            message(STATUS "Created ramses-shared-lib import target ramses::ramses")
        endif()
    endif()
endif()
